//
//  MSUserEditNameController.m
//  MSJD
//
//  Created by 箭军刘 on 2018/12/16.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSUserEditNameController.h"

#import "MSUserCenterDataSource.h"

@interface MSUserEditNameController ()<UITextFieldDelegate>

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UILabel *tipLabel;
@property (nonatomic, strong) UITextField *nameText;

@end

@implementation MSUserEditNameController

+ (instancetype)loadUserEditNameController {
    MSUserEditNameController *userEditNameVC = [[MSUserEditNameController alloc] init];
    return userEditNameVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initNavigationBar];
    [self initSubViews];
}

#pragma mark - Init Method

- (void)initNavigationBar {
    self.navigationItem.title = @"修改昵称";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(backButtonClick)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(saveButtonClick)];
}

- (void)initSubViews {

    self.view.backgroundColor = rgb(242, 242, 242);
    [self.view addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.offset(50);
    }];
    
    [self.contentView addSubview:self.tipLabel];
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.centerY.equalTo(self.contentView.mas_centerY);
    }];
    
    [self.contentView addSubview:self.nameText];
    [self.nameText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(60);
        make.right.offset(-10);
        make.centerY.equalTo(self.contentView.mas_centerY);
    }];
    self.nameText.text = [MSSafeObject stringInfo:[UserAccesser loadAccount].username];
    [self.nameText becomeFirstResponder];
}

#pragma mark - Event Response

- (void)backButtonClick {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)saveButtonClick {
    if ([MSSafeObject isNull:self.nameText.text]) {
        [MBProgressHUD showToastWithMessage:@"昵称不能为空" toView:self.view];
        return;
    }
    NSString *userName = self.nameText.text;
    [MBProgressHUD showMessage:@""];
    self.navigationItem.rightBarButtonItem.enabled = NO;
    _WEAKSELF
    [MSUserCenterDataSource modifyUserInfoWithAvatar:nil userName:userName gender:0 success:^(NSDictionary * _Nonnull responseInfo) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showToastWithMessage:@"修改昵称成功" toView:weakSelf.view];
        // 修改本地账号数据
        MSUser *loginUser = [UserAccesser loadAccount];
        loginUser.username = userName;
        [UserAccesser saveAccountToLocal:loginUser];
        [weakSelf backButtonClick];
    } failure:^(NSError * _Nonnull error) {
        NSString *errorMsg = error.localizedDescription?:@"修改昵称失败";
        [MBProgressHUD showToastWithMessage:errorMsg toView:weakSelf.view];
    }];
}

- (void)textFieldValueChanged:(UITextField *)textField {
    if ([MSSafeObject isNull:textField.text]) {
        self.navigationItem.rightBarButtonItem.enabled = NO;
    } else {
        self.navigationItem.rightBarButtonItem.enabled = YES;
    }
}

#pragma mark - Setters

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = [UIColor whiteColor];
    }
    return _contentView;
}

- (UILabel *)tipLabel {
    if (!_tipLabel) {
        _tipLabel = [[UILabel alloc] init];
        _tipLabel.text = @"昵称";
        _tipLabel.font = [UIFont systemFontOfSize:14];
        _tipLabel.textColor = [UIColor colorWithHexString:@"#4A4A4A"];
    }
    return _tipLabel;
}

- (UITextField *)nameText {
    if (!_nameText) {
        _nameText = [[UITextField alloc] init];
        _nameText.borderStyle = UITextBorderStyleNone;
        _nameText.clearButtonMode = UITextFieldViewModeWhileEditing;
        _nameText.placeholder = @"请输入昵称";
        [_nameText addTarget:self action:@selector(textFieldValueChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _nameText;
}

#pragma mark -  Memory Method

- (void)dealloc {
    NSLog(@"%s", __func__);
}

@end
